﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit.Framework;
using Locator.Stores;
using Stores.Tests.Mocks;
using System.Threading;
using System.Security.Principal;
using DataAccess.UnitTests;

namespace Stores.Tests
{
    [TestFixture]
    public class StoresControllerTests : DataTestBase
    {
        [Test]
        public void AllCitiesTest()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                var cities = controller.AllCities();
                Assert.AreEqual(4, cities.Count);
                Assert.AreEqual(Constants.Springfield, cities[0]);
            }
        }

        [Test]
        public void StoresInCityTest()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresInCity(Constants.NorthHaverbrook);
                Assert.AreEqual(1, stores.Count);
            }
        }

        [Test]
        public void StoresByPostalCodeSmallRadius()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresByPostalCode("L6H 6J1", 1);
                Assert.AreEqual(0, stores.Count);
            }
        }

        [Test]
        public void StoresByPostalCodeLargeRadius()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresByPostalCode("L6H 6J1", 10);
                Assert.AreEqual(1, stores.Count);
            }
        }

        [Test]
        public void StoresByLatLongSmallRadius()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresByLatitudeAndLongitude((decimal)Constants.HomeLatitude, (decimal)Constants.HomeLongitude, 1);
                Assert.AreEqual(0, stores.Count);
            }
        }

        [Test]
        public void StoresByLatLongLargeRadius()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresByLatitudeAndLongitude((decimal)Constants.HomeLatitude, (decimal)Constants.HomeLongitude, 10);
                Assert.AreEqual(1, stores.Count);
            }
        }

        [Test]
        public void StoresByPersonalLocationSmallRadius()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresByPersonalLocation(Constants.PersonalLocationId, 1);
                Assert.AreEqual(0, stores.Count);
            }
        }

        [Test]
        public void StoresByPersonalLocationLargeRadius()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresByPersonalLocation(Constants.PersonalLocationId, 10);
                Assert.AreEqual(1, stores.Count);
            }
        }

        [Test]
        public void StoresForUser()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.GetStoresForCurrentUser();
                Assert.AreEqual(3, stores.Count);
            }
        }

        [Test]
        public void StoresNotUsedByUser()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.FindStoresNotUsedByCurrentUser();
                Assert.AreEqual(2, stores.Count);
            }
        }

        [Test]
        public void AddAndDeleteStore()
        {
            using (StoresController controller = new StoresController(new MockGeoServiceAgent(), this.UnitOfWork))
            {
                Thread.CurrentPrincipal = new GenericPrincipal(new GenericIdentity(Constants.UserName), new string[] { });
                var stores = controller.GetStoresForCurrentUser();
                Assert.AreEqual(3, stores.Count);
                controller.AddStoreForCurrentUser(998);
                stores = controller.GetStoresForCurrentUser();
                Assert.AreEqual(4, stores.Count);
                controller.DeleteStoreForCurrentUser(998);
                stores = controller.GetStoresForCurrentUser();
                Assert.AreEqual(3, stores.Count);
            }
        }
    }
}
